package com.cloud.code.牛客网;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Test30 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            String line = scanner.nextLine();
            String[] chars = line.split(" ");
            int [] ints = new int[chars.length];
            for (int i = 0; i < chars.length; i++) {
                ints[i] = Integer.parseInt(chars[i]);
            }
            int root = Integer.parseInt(chars[0]);
            List<Integer> list = new ArrayList<>();

            int count = 0;
            int dfs = dfs(1,ints,root, count);

            System.out.println(dfs+1);


        }
    }

    private static int dfs(int n,int[] chars,int root, int count) {



        int left = 2*n;
        int right = 2*n+1;

        if(left > chars.length-1){
            return count;
        }

        if(chars[left-1] != -1 && chars[left-1] < root && chars[right-1] != -1 && chars[right-1] > root){
            count ++;
            int dfs = dfs(left, chars, chars[left-1], count);
            int dfs2 = dfs(left, chars, chars[left-1], count);
            return Math.min(dfs,dfs2);
        }else if(chars[left-1] != -1 && chars[left-1] < root){
            count++;
            return  dfs(left, chars, chars[left-1], count);
        }else if(chars[right-1] != -1 && chars[right-1] > root){
            count++;
            return dfs(right, chars, chars[right-1], count);
        }else {
            return count;
        }
    }

}
